import os,sys,random,pygame,math
from pygame.locals import *
import cPickle as pickle

import screen

def load_image(name,colorkey):
    fullname = os.path.join('data','pictures',name)
    i=pygame.image.load(fullname)
    if colorkey==0:
        colorkey=None
    if colorkey is not None:
        if colorkey is -1:
            colorkey = i.get_at((0,0))
        i.set_colorkey(colorkey, RLEACCEL)
    return i.convert()

class ParticleNoGravity:
    def __init__(self,pos,image):
        self.pos = pos
        self.image = image
        self.x = random.randint(pos[0]-12,pos[0]+12)
        self.y = random.randint(pos[1]-12,pos[1]+12)
        degree = random.randint(0,360)
        speed = random.randint(1,100)/150.0
        self.transparency = random.randint(150,200)
        self.image.set_alpha(self.transparency)
        self.angle = random.randint(0,360)
        self.rotation_speed = (random.randint(0,200)-100)/100.0
        self.move = (speed*math.sin(math.radians(degree)),speed*math.cos(math.radians(degree)))
        self.original = self.image
                                                                    
    def update(self):
        self.x += self.move[0]
        self.y += self.move[1]
        screen.screen.blit(self.image,(self.x,self.y))
        self.transparency -= 7
        self.original.set_alpha(self.transparency)
        if self.transparency <= 0:
            self.transparency = 0

        self.angle += self.rotation_speed
        self.image = self.original
        self.image = pygame.transform.rotate(self.image,self.angle)

    def move_it(self,move):
        self.x+=move[0]
        self.y+=move[1]

class ParticleController:
    def __init__(self):
        self.particles = []
        for x in range(self.particle_number):
            if self.gravity_bound == 0:
                self.particles.append(ParticleNoGravity(self.pos,self.image))

    def update(self):
        self.counter -= 1
        for particle in self.particles:
            particle.move_it(self.gravity)
            particle.update()

    def move(self,move):
        for item in self.particles:
            item.move_it(move)

class PlayerLanding(ParticleController):
    def __init__(self, pos):
        self.pos = pos
        self.image = load_image('smoke.png',-1)
        self.counter = 30
        self.particle_number = 8
        self.spin = True
        self.gravity_bound = 0
        self.gravity = (0,-.05)
        ParticleController.__init__(self)

            
